import { ui } from "./../ui/layaMaxUI";
import { BlurEffect } from "../effect/BlurEffect";
import CameraMoveScript from "./CameraMove";
/**
 * 本示例采用非脚本的方式实现，而使用继承页面基类，实现页面逻辑。在IDE里面设置场景的Runtime属性即可和场景进行关联
 * 相比脚本方式，继承式页面类，可以直接使用页面定义的属性（通过IDE内var属性定义），比如this.tipLbll，this.scoreLbl，具有代码提示效果
 * 建议：如果是页面级的逻辑，需要频繁访问页面内多个元素，使用继承式写法，如果是独立小模块，功能单一，建议用脚本方式实现，比如子弹脚本。
 */
export default class GameUI extends ui.test.TestSceneUI 
{
    private _scene3d:Laya.Scene3D;
    private _camera:Laya.Camera;
    private _postPro:Laya.PostProcess;
    private _quad:Laya.MeshSprite3D;
    private _tex:Laya.Texture;
    private _mat:Laya.UnlitMaterial;

    private _tempRT:Laya.RenderTexture;

    onAwake()
    {
        this.initScene3d();
        this.initCube();
        this.initBlurEffect();
        this.initQuad(Laya.stage.width, Laya.stage.height);
        this._quad && (this._quad.active = false);

        this.btnEffect.on(Laya.Event.CLICK, this, this.onBtnSwitchClick);
        this.btnSnap.on(Laya.Event.CLICK, this, this.onBtnSnapClick);
        this.bgColor.visible = true;

        window['testUI'] = this;
    }
    public set tex(tex:Laya.Texture)
    {
        this._tex = tex;
        if (tex && this._mat) this._setQuadTex(tex);
    }

    onBtnSwitchClick()
    {
        let camera = this._camera;
        if (!camera) return;
        camera.postProcess ? camera.postProcess = null : camera.postProcess = this._postPro;
    }

    onBtnSnapClick()
    {
        this._takSnap();
    }

    _takSnap()
    {
        let camera = this._camera;
        if (!camera) return;
        this._dispose();
        this.bgColor.visible = true;
        this._quad && (this._quad.active = false);
        let tex = this.stage.drawToTexture(Laya.stage.width, Laya.stage.height, 0, 0);
        //this.bgColor.visible = false;
        this.tex = tex as Laya.Texture;
        this._quad && (this._quad.active = true);
        let target = Laya.RenderTexture.createFromPool(tex.width, tex.height,Laya.Texture2D.FORMAT_R8G8B8, Laya.Texture2D.FORMAT_DEPTHSTENCIL_NONE);
        camera.renderTarget = target;
        camera.postProcess = this._postPro;
        camera.renderingOrder = -10;
        this._tempRT = target;
        this.timer.frameOnce(1, this, this._defer);
    }

    _defer()
    {
        let camera = this._camera;
        let tex = camera.renderTarget;
        //Laya.RenderTexture.recoverToPool()
        camera.renderTarget = null;
        camera.postProcess = null;
        tex && this._setQuadTex(tex);
        if (this._quad) {
            this._quad.transform.localScaleY > 0 ? this._quad.transform.localScaleY = -1 : this._quad.transform.localScaleY = 1;
        }
    }

    initScene3d()
    {
        let s3d = this._scene3d;
        if (!s3d) {
            s3d = this._scene3d = new Laya.Scene3D();
            this.s3droot.addChild(s3d);
        }

        let camera = this._camera;
        if (!camera) {
            camera = this._camera = new Laya.Camera(0, .1, 100);
            camera.enableHDR = false;
            camera.orthographic = true;
            camera.clearFlag = Laya.BaseCamera.CLEARFLAG_DEPTHONLY;
            camera.transform.translate(new Laya.Vector3(0, 0, 10));
            s3d.addChild(camera);
        }

        camera.addComponent(CameraMoveScript);
    }

    initCube()
    {
        let boxMesh = new Laya.MeshSprite3D(Laya.PrimitiveMesh.createBox(1, 1, 1));
        let boxMat = new Laya.BlinnPhongMaterial();
        boxMesh.meshRenderer.sharedMaterial = boxMat;
        Laya.Texture2D.load("res/layabox.png", Laya.Handler.create(null, function(tex:Laya.Texture2D) {
            boxMat.albedoTexture = tex;
        }));

        this._scene3d.addChild(boxMesh);
        boxMesh.transform.translate(new Laya.Vector3(0, -3, 5));
        boxMesh.transform.rotate(new Laya.Vector3(0, 45, 0), false, false);

        //添加方向光
        let directionLight: Laya.DirectionLight = this._scene3d.addChild(new Laya.DirectionLight()) as Laya.DirectionLight;
        directionLight.color = new Laya.Vector3(0.6, 0.6, 0.6);
        directionLight.transform.worldMatrix.setForward(new Laya.Vector3(1, -1, 0));
    }

    initBlurEffect()
    {
        BlurEffect.init();
        let camera = this._camera;
        let postpro = new Laya.PostProcess();
        let blurEffect = new BlurEffect();
        postpro.addEffect(blurEffect);
        camera.postProcess = postpro;
        this._postPro = postpro;

        //设置模糊参数
        blurEffect.downSampleNum  = 4;
        blurEffect.blurSpreadSize = 1;
        blurEffect.blurIterations = 1;

        window['blurEffect'] = blurEffect;
    }

    initQuad(w:number, h:number)
    {
        if (!this._quad) {
            let camera = this._camera;
            let scale = camera.orthographicVerticalSize;
            w = w / h * scale;
            let quad = new Laya.MeshSprite3D(Laya.PrimitiveMesh.createQuad(w, scale));
            let material = new Laya.UnlitMaterial();
            quad.meshRenderer.sharedMaterial = material;
            this._mat = material;
            this._scene3d.addChild(quad);
            this._quad = quad;
        }
    }

    _setQuadTex(tex:Laya.Texture | Laya.BaseTexture)
    {
        if (!tex) {
            this._mat.albedoTexture = null;
        } else {
            this._mat.albedoTexture = tex instanceof Laya.BaseTexture ? tex : tex.bitmap as Laya.BaseTexture;
        }
    }

    _dispose()
    {
        this._mat && (this._mat.albedoTexture = null);
        this._tempRT && (Laya.RenderTexture.recoverToPool(this._tempRT));
        this._tempRT = null;
        this._tex && this._tex.destroy();
        this._tex = null;
    }
}